﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.ComponentModel;

namespace BlueMarble.Genesis.Infrastructure.Background.Dynamic
{
    /// <summary>
    /// Interaction logic for Background.xaml
    /// </summary>
    public partial class Background : UserControl, INotifyPropertyChanged
    {
        protected const int ScreenWidth = 1920;
        protected const int ScreenHeight = 1080;

        protected System.Windows.Threading.DispatcherTimer dispatcherTimer;

        public Background()
        {
            InitializeComponent();

            this.DataContext = this;

            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();

            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1500);
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Start();
        }

        void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            dispatcherTimer.Stop();

            LayerNear.PopulateShapes(10);
            LayerMiddle.PopulateShapes(20);
            LayerFar.PopulateShapes(45);
        }

        private void BackgroundRoot_MouseMove(object sender, MouseEventArgs e)
        {
            Point pt = e.GetPosition(this);

            this.ProcessMouseMove(pt);
        }

        public void ProcessMouseMove(Point MouseLocation)
        {
            double xPercent = MouseLocation.X / 1600;
            double yPercent = MouseLocation.Y / 900;

#if SILVERLIGHT
            this.Translate1ControlsX.Value = -0.25 * 800d * xPercent;
            this.Translate1ControlsY.Value = -0.25 * 800d * yPercent;

            this.Translate1NearX.Value = -1 * 400d * xPercent;
            this.Translate1NearY.Value = -1 * 400d * yPercent;

            this.Translate1MiddleX.Value = -1 * 300d * xPercent;
            this.Translate1MiddleY.Value = -1 * 300d * yPercent;

            this.Translate1FarX.Value = -1 * 200d * xPercent;
            this.Translate1FarY.Value = -1 * 200d * yPercent;

            this.Translate1BackgroundX.Value = -1 * 200d * xPercent;
            this.Translate1BackgroundY.Value = -1 * 200d * yPercent;

            //this.StoryboardControls.Begin();

            this.StoryboardBackground.Begin();
            this.StoryboardFar.Begin();
            this.StoryboardMiddle.Begin();
            this.StoryboardNear.Begin();
#else
            Storyboard controlsStoryboard = (Storyboard)this.Resources["StoryboardControlsResource"];

            if (controlsStoryboard != null)
            {
                DoubleAnimationUsingKeyFrames controlsXDoubleAnimationKeyFrame = (controlsStoryboard.Children[0] as DoubleAnimationUsingKeyFrames);
                DoubleAnimationUsingKeyFrames controlsYDoubleAnimationKeyFrame = (controlsStoryboard.Children[1] as DoubleAnimationUsingKeyFrames);

                controlsXDoubleAnimationKeyFrame.KeyFrames[0].Value = -0.25d * 800d * xPercent;
                controlsYDoubleAnimationKeyFrame.KeyFrames[0].Value = -0.25d * 800d * yPercent;
            }

            Storyboard nearStoryboard = (Storyboard)this.Resources["StoryboardNearResource"];

            if (nearStoryboard != null)
            {
                DoubleAnimationUsingKeyFrames nearXDoubleAnimationKeyFrame = (nearStoryboard.Children[0] as DoubleAnimationUsingKeyFrames);
                DoubleAnimationUsingKeyFrames nearYDoubleAnimationKeyFrame = (nearStoryboard.Children[1] as DoubleAnimationUsingKeyFrames);

                nearXDoubleAnimationKeyFrame.KeyFrames[0].Value = -1d * 400d * xPercent;
                nearYDoubleAnimationKeyFrame.KeyFrames[0].Value = -1d * 400d * yPercent;
            }

            Storyboard middleStoryboard = (Storyboard)this.Resources["StoryboardMiddleResource"];

            if (middleStoryboard != null)
            {
                DoubleAnimationUsingKeyFrames middleXDoubleAnimationKeyFrame = (middleStoryboard.Children[0] as DoubleAnimationUsingKeyFrames);
                DoubleAnimationUsingKeyFrames middleYDoubleAnimationKeyFrame = (middleStoryboard.Children[1] as DoubleAnimationUsingKeyFrames);

                middleXDoubleAnimationKeyFrame.KeyFrames[0].Value = -1d * 300d * xPercent;
                middleYDoubleAnimationKeyFrame.KeyFrames[0].Value = -1d * 300d * yPercent;
            }

            Storyboard farStoryboard = (Storyboard)this.Resources["StoryboardFarResource"];

            if (farStoryboard != null)
            {
                DoubleAnimationUsingKeyFrames farXDoubleAnimationKeyFrame = (farStoryboard.Children[0] as DoubleAnimationUsingKeyFrames);
                DoubleAnimationUsingKeyFrames farYDoubleAnimationKeyFrame = (farStoryboard.Children[1] as DoubleAnimationUsingKeyFrames);

                farXDoubleAnimationKeyFrame.KeyFrames[0].Value = -1d * 200d * xPercent;
                farYDoubleAnimationKeyFrame.KeyFrames[0].Value = -1d * 200d * yPercent;
            }

            Storyboard backgroundStoryboard = (Storyboard)this.Resources["StoryboardBackgroundResource"];

            if (backgroundStoryboard != null)
            {
                DoubleAnimationUsingKeyFrames backgroundXDoubleAnimationKeyFrame = (backgroundStoryboard.Children[0] as DoubleAnimationUsingKeyFrames);
                DoubleAnimationUsingKeyFrames backgroundYDoubleAnimationKeyFrame = (backgroundStoryboard.Children[1] as DoubleAnimationUsingKeyFrames);

                backgroundXDoubleAnimationKeyFrame.KeyFrames[0].Value = -1d * 200d * xPercent;
                backgroundYDoubleAnimationKeyFrame.KeyFrames[0].Value = -1d * 200d * yPercent;
            }

            //controlsStoryboard.Begin();
            nearStoryboard.Begin();
            middleStoryboard.Begin();
            farStoryboard.Begin();
            backgroundStoryboard.Begin();
#endif

        }

        public Brush BackgroundLayerBrush
        {
            get { return (Brush)GetValue(BackgroundLayerBrushProperty); }
            set { SetValue(BackgroundLayerBrushProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BackgroundLayerBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BackgroundLayerBrushProperty =
            DependencyProperty.Register("BackgroundLayerBrush", typeof(Brush), typeof(Background), new PropertyMetadata(null));

        public ShapeBase Shape
        {
            get { return (ShapeBase)GetValue(ShapeProperty); }
            set { SetValue(ShapeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Shape.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShapeProperty =
            DependencyProperty.Register("Shape", typeof(ShapeBase), typeof(Background), new PropertyMetadata(null));

        public UIElement Content
        {
            get { return (UIElement)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Content.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(UIElement), typeof(Background), new PropertyMetadata(null));

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler Handler = PropertyChanged;
            if (Handler != null) Handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
